// packageC/pages/package/OCR_namage/edit/index.js
const mybehavior = require("../../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/ocr_calling_api/gaolan_ocr_bp",
    row_data: {},
    mode_show: false,
    rate_show: false,
    address_show: false,
    description_show: false,
    params_show: false,
    admin_show: false,
    mode_list: ["绘图模式", "工作模式"],
    mode: "",
    submit_disabled: false,
    rate: 1,
    address: "",
    admin_email_list: [],
    params_list: []
  },
  // 单选下拉框下拉框
  getSelectRadioValue(e) {
    let key = e.currentTarget.dataset.id
    this.setData({
      [key]: e.detail.value
    })
  },

  submitMode() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    if (this.data.mode == "") {
      wx.showToast({
        title: '请选择工作模式！',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_mode";
      let data = {
        uid: this.data.row_data._id,
        mode: this.data.mode == '绘图模式' ? 1 : 2,
      }

      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },
  // 修改时间间隔
  stepChange(e) {
    this.setData({
      rate: e.detail
    })
    // if (e.detail < 60) {
    //   this.setData({
    //     rate: 60
    //   })
    // }
  },
  // 修改时间间隔
  submitRate() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    if (this.data.rate == this.data.row_data.rate) {
      wx.showToast({
        title: '数据无修改！',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_rate";
      let data = {
        uid: this.data.row_data._id,
        rate: this.data.rate,
      }

      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },
  // 维护安装位置
  submitAddress() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    if (this.data.address == "") {
      wx.showToast({
        title: '请输入最新安装位置',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_address";
      let data = {
        uid: this.data.row_data._id,
        address: this.data.address,
      }
      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },
  // 维护设备详细描述
  submitDescription() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    if (this.data.description == "") {
      wx.showToast({
        title: '请输入最新设备详细描述',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_description";
      let data = {
        uid: this.data.row_data._id,
        description: this.data.description,
      }
      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },
  // 增加一行
  addAdmin() {
    let arr = this.data.admin_email_list
    arr.push("")
    this.setData({
      admin_email_list: arr
    })
  },
  // 删除一行
  deleteAdmin(e) {
    console.log(e);
    let arr = this.data.admin_email_list
    let index = e.currentTarget.dataset.index
    arr.splice(index, 1)
    this.setData({
      admin_email_list: arr
    })
  },
  // 双向绑定管理员邮箱值
  bindAdminDatas(e) {
    let arr = this.data.admin_email_list
    let index = e.currentTarget.dataset.index
    arr[index] = e.detail
    this.setData({
      admin_email_list: arr
    })
  },
  // 维护设备管理员邮箱号
  submitAdmin() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    if (this.data.admin_email_list.includes("")) {
      wx.showToast({
        title: '请删除空值列再提交',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_admin_email";
      let data = {
        uid: this.data.row_data._id,
        admin_emails: this.data.admin_email_list,
      }

      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },

  // 增加一列库位坐标数据
  addParams() {
    let arr = this.data.params_list
    let obj = {
      al: "",
      lt: [],
      rb: [],
      url: "",

    }
    arr.push(obj)
    this.setData({
      params_list: arr
    })
  },
  // 删除一行
  deleteParams(e) {
    let arr = this.data.params_list
    let index = e.currentTarget.dataset.index
    arr.splice(index, 1)
    this.setData({
      params_list: arr
    })
  },
  // 绑定数据
  paramsDatas(e) {
    let arr = this.data.params_list
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    arr[index][id] = e.detail
    this.setData({
      params_list: arr
    })
    console.log("xxxx", this.data.params_list);
  },
  // 绑定数据
  paramsDatas2(e) {
    let val = e.detail

    let arr = this.data.params_list
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let xb = e.currentTarget.dataset.xb
    if (e.detail == "") {
      arr[index][id][xb] = val
    } else {

      arr[index][id][xb] = parseInt(val)
    }
    this.setData({
      params_list: arr
    })
    console.log("xxxx", this.data.params_list);
  },
  // 判断对象里的值是否有空
  hasEmptyValues(list) {
    return list.some(item => {
      return Object.values(item).some(value => value === null || value === undefined || value === '' || value.length == 0);
    });
  },
  // 设置库位坐标
  submitParams() {
    this.setData({
      submit_disabled: true,
    })
    wx.showLoading({
      title: '处理中...',
    })
    const flag = this.hasEmptyValues(this.data.params_list)
    if (flag) {
      wx.showToast({
        title: '请完善数据!',
        icon: 'none'
      })
      this.setData({
        submit_disabled: false,
      })
      wx.hideLoading()
    } else {
      let url = this.data.baseUrl + "/set_params";
      let data = {
        uid: this.data.row_data._id,
        params: this.data.params_list,
      }

      this.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {

          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(() => {
            wx.reLaunch({
              url: '/packageC/pages/package/OCR_namage/index',
            })
          }, 500)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        this.setData({
          submit_disabled: false,
        })
        wx.hideLoading()
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('setMode', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        mode_show: true,
      })

    })
    eventChannel.on('setRate', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        rate_show: true,
      })

    })
    eventChannel.on('setAddress', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        address_show: true,
      })

    })
    eventChannel.on('setDescription', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        description_show: true,
      })

    })
    eventChannel.on('setAdmin', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        admin_show: true,
        admin_email_list: data.admin_email
      })

    })
    eventChannel.on('setParams', function (data) {
      console.log(data);

      self.setData({
        row_data: data,
        params_show: true,
        params_list: data.params
      })

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})